Import Libraries


In [1]:
import cv2

In [2]:
import matplotlib.pyplot as plt

Read Images


In [3]:
shooter = cv2.imread('demo1.jpg', cv2.IMREAD_GRAYSCALE)

In [4]:
nature = cv2.imread('nature.jpg', cv2.IMREAD_GRAYSCALE)

In [5]:
plt.figure(figsize=(20,15))


Out[5]:
<matplotlib.figure.Figure at 0x7f12c336de10>

In [6]:
plt.subplot(1,2,1)


Out[6]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f12c336de48>

In [7]:
plt.title('Shooter')


Out[7]:
<matplotlib.text.Text at 0x7f12c3089320>

In [8]:
plt.imshow(shooter, cmap='gray')


Out[8]:
<matplotlib.image.AxesImage at 0x7f12c30343c8>

In [9]:
plt.subplot(1,2,2)


Out[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f12c3034320>

In [10]:
plt.title('Nature')


Out[10]:
<matplotlib.text.Text at 0x7f12c3001be0>

In [11]:
plt.imshow(nature, cmap='gray')


Out[11]:
<matplotlib.image.AxesImage at 0x7f12c2fb48d0>

In [12]:
plt.show()


Simple image Difference


In [13]:
img = shooter - nature

In [14]:
plt.imshow(img, cmap='gray')


Out[14]:
<matplotlib.image.AxesImage at 0x7f12bee35f98>

In [15]:
plt.show()


Image looks absolutely unrecognizable here. It is due to the fact that, when d = (a-b<0), d = 0 because negative intensity value gets truncated to 0. Even, using abs() won't help, as a-b subtration will happen first and yeild 0 , and then abs(0) will happen, which has no meaning. This can be solved by using absdiff function of cv2.

Absolute Image Difference


In [16]:
img_abs = cv2.absdiff(shooter, nature)

In [17]:
plt.imshow(img_abs, cmap='gray')


Out[17]:
<matplotlib.image.AxesImage at 0x7f12bedd0d68>

In [18]:
plt.show()


Finally,a little better image.